//
//  XYComposeTextController.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/21.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYComposeTextController: UIViewController ,UITextViewDelegate , XYInputToolBarDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        //添加nav的按钮
        setNav()
        //添加界面控件
        setTextView()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        textInputView.becomeFirstResponder()
    }
    
    //添加nav的控件
    private func setNav(){
        //添加左侧取消按钮
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "didCancelButton")
        
        self.navigationItem.titleView = self.titleView
        
        //添加右侧完成按钮,并设置成禁止状态
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: doneButton)
        self.navigationItem.rightBarButtonItem?.enabled = false
    }
    
    //取消按钮的点击事件
    @objc private func didCancelButton(){
        
        textInputView.resignFirstResponder()

         dismissViewControllerAnimated(true, completion: nil)
     
    }
    
    //界面控件
    private func  setTextView(){
        //添加输入框
        textInputView.size = CGSizeMake(SCREENW, SCREENH)
        view.addSubview(textInputView)
        
        //添加图片框
        addImageView.x = 10
        addImageView.y = 200
        view.addSubview(addImageView)
        //添加图片框的闭包
        addImageView.addButtonBlock = {
            self.setImageToAddImageView()
        }
        
        //添加键盘工具条
        inputToolBar.y = SCREENH - inputToolBar.height
        view.addSubview(inputToolBar)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillChang:", name: UIKeyboardWillChangeFrameNotification, object: nil)
    }
    
    
    //通知方法
    @objc private func  keyBoardWillChang(not:NSNotification){
        
//        printLog(not)
        //键盘移动的位置
        let rect = (not.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
        
        //键盘移动的时间
        let time = (not.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        
        UIView.animateWithDuration(time) { () -> Void in
            self.inputToolBar.y = rect.origin.y - self.inputToolBar.height
        }
    }
    
    
    //textView的代理方法
    func textViewDidChange(textView: UITextView) {
        self.navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    //拖动输入框,撤销键盘
    func scrollViewDidScroll(scrollView: UIScrollView) {
        textInputView.resignFirstResponder()
    }
    

    
    //toolbar的代理方法
      func didInputToolBarButton(toolbar: XYInputToolBar, type: XYInputToolBarType) {
        
        switch type{
        case .Picture :
            //点击按钮之后,添加图片
            setImageToAddImageView()
        case .Mention :
            printLog("@")
        case .Trend :
            printLog("主题")
        case .Emoticon :
            setEmoKeybord()
        case .Add :
            printLog("加号")
        }
    }
    
    
    //toolBar的按钮点击事件
    //点击了添加图片按钮
    private func setImageToAddImageView(){
        //创建一个图片的控制器
        let imagePicker = UIImagePickerController()
        //指定从什么地方加载图片(相册)
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        //设置代理
        imagePicker.delegate = self
        
        presentViewController(imagePicker, animated: true, completion: nil)
        
    }
    
    //MARK : - Picker的代理方法
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        addImageView.addImage(image.Scale(300))
        dismissViewControllerAnimated(true, completion: nil)

    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController){
        printLog("点击了取消")
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    //点击了表情按钮
    private func setEmoKeybord(){
        //取消第一响应者
        self.textInputView.resignFirstResponder()
        
        self.textInputView.inputView = textInputView.inputView == nil ? emoKeyboard : nil
        
        //成为第一响应者
        self.textInputView.becomeFirstResponder()
    }
    

    //titleview控件
    private lazy var titleView:UILabel = {
    
        let label  = UILabel()
        //设置换行
        label.numberOfLines = 0
        //设置文字居中
        label.textAlignment = NSTextAlignment.Center
        
        //取出模型中的用户姓名
        if let labelName = XYWebModel.loadUserAccount()?.screen_name{
            let title = "发微博\n\(labelName)"
            
            //给label设置文字属性
            var attr = NSMutableAttributedString(string: title)
            
            let rang = (title as NSString).rangeOfString(labelName)
            //添加字体属性
            attr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(14), range: rang)
            attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: rang)
            
            label.attributedText = attr
        }else{
            label.text = "发微博"
        }
        
        label.sizeToFit()
        return label
    }()
    
    //加载右侧的完成按钮
    private lazy var doneButton:UIButton = {
        
        let button = UIButton()
        //设置title
        button.setTitle("发送", forState: UIControlState.Normal)
        //设置不同状态下得文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Disabled)
        
        //设置图片
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Disabled)
        
        button.size = CGSizeMake(44, 30)
        button.titleLabel!.font = UIFont.systemFontOfSize(14)
        
        return button
        
    }()
    
    //加载文字输入栏
    private lazy var textInputView:XYComposeTextInputView = {
        
        let textView = XYComposeTextInputView()
        textView.font = UIFont.systemFontOfSize(16)
        textView.alwaysBounceVertical = true
        
        textView.placeholder = "你猜我猜不猜"
        textView.delegate = self
        return textView
    }()
    
    //加载工具栏
    private lazy var inputToolBar:XYInputToolBar = {
        let view = XYInputToolBar()
        
        view.height = 44
        view.width = SCREENW
        view.delegate = self
        return view
    }()
    
    //加载图片框
    private lazy var addImageView:XYPhotoView = {
        
        let view = XYPhotoView()
        
        view.width = SCREENW - 2 * 10
        view.height = view.width
        
        return view
    }()
    
    //加载表情键盘
    private var emoKeyboard:XYEmoKeyboard = {
        let view = XYEmoKeyboard()
        view.size = CGSizeMake(SCREENW, 216)

        return view
    }()

}




